package com.LatitudeYao;

import java.lang.*;
/**
 * Created by GTS on 2017/7/13.
 * 从右上角开始查找，如果等于则直接 true,r如果大于target则在左侧列中查找。如果小于target则在下方的区域查找
 * 注意判空
 */
class FindTarget{
    public  boolean find(int[][] array,int target) {
        if (array == null) {
            return false;
        }
        int column = array[0].length - 1;
        int row = 0;
        while (row < array.length && column >= 0) {
            if (array[row][column] == target) {
                return true;
            }
            if (array[row][column] > target) {
                column--;
            } else {
                row++;
            }
        }
        return false;
        }
}

public class ArraySearchYM {
    public static void main(String[] args) {
        int[][] array = new int[4][4];
        int count =0;
        for(int i=0;i<4;i++){
            for (int j = 0; j <4 ; j++) {
                array[i][j]=count;
                count++;
            }
        }
        FindTarget ft = new FindTarget();
        boolean findTarget = ft.find(array,4);
        System.out.println(findTarget);
    }
}
